package com.example.demo;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.ClientInfoStatus;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {

    /**
     * 传递单个参数
     * @param s1
     * @return
     */
    @RequestMapping("/r1")
    public String r1(String s1) {
        return "接收到参数: " + s1;
    }

    @RequestMapping("/r2")
    public String r2(Integer age) {
        return "接收到参数: " + age;
    }

    @RequestMapping("/r3")
    public String r3(Boolean flag) {
        return "接收到参数: " + flag;
    }

    /**
     * 传递多个参数
     * @param name
     * @param age
     * @param gender
     * @return
     */
    @RequestMapping("/r4")
    public String r4(String name, Integer age, String gender) {
        return String.format("姓名:%s, 年龄:%d, 性别:%s",name,age,gender);
    }

    /**
     * 传递对象
     * @param person
     * @return
     */
    @RequestMapping("/r5")
    public String r5(Person person) {
        return "接收到参数: " + person;
    }

    /**
     * 后端参数重命名（后端参数映射）
     * @param name
     * @return
     */
    @RequestMapping("/r6")
    public String r6(@RequestParam(value = "xm", required = false) String name) {
        return "接收到参数: " + name;
    }

    /**
     * 传递数组
     * @param array
     * @return
     */
    @RequestMapping("/r7")
    public String r7(String[] array) {
        return "接收到参数: " + Arrays.toString(array);
    }

    /**
     * 传递集合
     * @param list
     * @return
     */
    @RequestMapping("/r8")
    public String r8(@RequestParam List<String> list) {
        return "接收到参数: " + list;
    }

    /**
     * 传递JSON数据
     * @param person
     * @return
     */
    @RequestMapping("/r9")
    public String r9(@RequestBody Person person) {
        return "接收到参数: " + person;
    }

    /**
     * 获取URL中参数@PathVariable
     * @param articleId
     * @return
     */
    // http://127.0.0.1:8080/request/1
    @RequestMapping("/{articleId}")
    public String r10(@PathVariable("articleId") Integer articleId) {
        return "接收到参数: " + articleId;
    }

    // http://127.0.0.1:8080/request/blog/1
    @RequestMapping("/{type}/{articleId}")
    public String r10(@PathVariable("articleId") Integer articleId, @PathVariable String type) {
        return "接收到参数: " + articleId + ", type: " + type;
    }

    /**
     * 上传⽂件@RequestPart
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/r11")
    public String r11(MultipartFile file) throws IOException {
        String name = file.getName();
        String originalFilename = file.getOriginalFilename();
        System.out.println("originalFilename: " + originalFilename);
        String contentType = file.getContentType();
        System.out.println("contentType: " + contentType);

        // 文件上传
        file.transferTo(new File("D:\\delete\\" + originalFilename));

        return "接收到文件: " + name;
    }


    
}
